<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\controller\api\v1\order;

use app\Request;
use app\services\order\StoreCartServices;
use app\services\activity\discounts\StoreDiscountsServices;
use app\services\store\SystemStoreServices;
use crmeb\services\CacheService;

/**
 * 购物车类
 * Class StoreCart
 * @package app\controller\api\store
 */
class StoreCart
{
    protected $services;

    public function __construct(StoreCartServices $services)
    {
        $this->services = $services;
    }

    /**
     * 购物车 列表1
     * @param Request $request
     * @return mixed
     */
    public function lst(SystemStoreServices $storeServices, Request $request)
    {
        [$status, $latitude, $longitude, $store_id] = $request->postMore([
            ['status', 1],//购物车商品状态
            ['latitude', ''],
            ['longitude', ''],
			['store_id', 0]
        ], true);
		if (!checkCoordinates($longitude, $latitude)) {
			return app('json')->fail('参数错误');
		}
		$uid = $request->hasMacro('uid') ? (int)$request->uid() : 0;
		$result = [];
		if ($uid) {
			$this->services->setItem('latitude', $latitude)->setItem('longitude', $longitude)->setItem('status', $status);
			$where = ['status' => $status, 'staff_id' => 0];
			$entryRules = $storeServices->getStoreIdByEntryRules($uid);
			$shop_operation_type = $entryRules['shop_operation_type'] ?? 1;
			$storeId = $entryRules['store_id'] ?? 0;
			switch ($shop_operation_type) {
				case 1://展示平台加入购物车商品
					$where['store_id'] = 0;
					break;
				case 2://展示平台+门店加入购物车商品
					$where['store_id'] = [0, $storeId];
					break;
				case 3://展示门店加入购物车商品
					$where['store_id'] = $storeId;
					break;
				default://展示平台加入购物车商品
					$where['store_id'] = 0;
					break;
			}

			$result = $this->services->getUserCartList($uid, $where);
			$this->services->reset();
			$result['valid'] = $this->services->getReturnCartList($result['valid'], $result['promotions']);
			unset($result['promotions']);
		}
        return app('json')->successful($result);
    }

    /**
     * 购物车 添加
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add(Request $request)
    {
        $where = $request->postMore([
            ['productId', 0],//普通商品编号
            [['cartNum', 'd'], 1], //购物车数量
            ['uniqueId', ''],//属性唯一值
            [['new', 'd'], 0],// 1 加入购物车直接购买  0 加入购物车
            [['is_new', 'd'], 0],// 1 加入购物车直接购买  0 加入购物车
            [['secKillId', 'd'], 0],//秒杀商品编号
            [['bargainId', 'd'], 0],//砍价商品编号
			[['combinationId', 'd'], 0],//拼团商品编号
			[['storeIntegralId', 'd'], 0],//积分商品ID
            [['discountId', 'd'], 0],//优惠套餐编号
            ['discountInfos', []],//优惠套餐商品信息
            [['newcomerId', 'd'], 0],//新人专享商品编号
            [['luckRecordId', 'd'], 0],//抽奖记录编号
			[['key', 's'], ''],//直接购买购物车ID 再次累加1
			[['is_set', 'd'], 0],//1：直接设置购物车数量 0：累加
        ]);
        if ($where['is_new'] || $where['new']) $new = true;
        else $new = false;
        if (!$where['productId'] && !$where['discountId']) {
            return app('json')->fail('参数错误');
        }
        $type = 0;
        $uid = (int)$request->uid();
		$activityId = 0;
		$this->services->setItem('key', $where['key'] ?? '');
		$this->services->setItem('is_set', $where['is_set'] ?? 0);
        if ($where['discountId']) {
            /** @var StoreDiscountsServices $discountService */
            $discountService = app()->make(StoreDiscountsServices::class);
            $discounts = $discountService->get((int)$where['discountId'], ['id', 'is_limit', 'limit_num']);
            if (!$discounts) {
                return app('json')->fail('套餐商品未找到！');
            }
            //套餐限量
            if ($discounts['is_limit']) {
                if ($discounts['limit_num'] <= 0) {
                    return app('json')->fail('套餐限量不足');
                }
                if (!CacheService::checkStock(md5($discounts['id']), 1, 5)) {
                    return app('json')->fail('套餐限量不足');
                }
            }
            $cartIds = [];
			$cartNum = 0;
            $activityId = (int)$where['discountId'];
            foreach ($where['discountInfos'] as $info) {
                [$cartId, $cartNum] = $this->services->setCart($uid, (int)$info['product_id'], 1, $info['unique'], 5, $new, $activityId, (int)$info['id']);
				$cartIds[] = $cartId;
            }
        } else {
            if ($where['secKillId']) {
                $type = 1;
                $activityId = $where['secKillId'];
            } elseif ($where['bargainId']) {
                $type = 2;
                $activityId = $where['bargainId'];
            } elseif ($where['combinationId']) {
                $type = 3;
                $activityId = $where['combinationId'];
            } elseif ($where['storeIntegralId']) {
				$type = 4;
				$activityId = $where['storeIntegralId'];
			} elseif ($where['newcomerId']) {
				$type = 7;
                $activityId = $where['newcomerId'];
            } elseif ($where['luckRecordId']) {
				$type = 8;
                $activityId = $where['luckRecordId'];
            }
            [$cartIds, $cartNum] = $this->services->setCart($uid, (int)$where['productId'], (int)$where['cartNum'], $where['uniqueId'], $type, $new, (int)$activityId);
        }
		$this->services->reset();
        if (!$cartIds) {
			return app('json')->fail('添加失败');
		} else  {
			//更新秒杀详情缓存
			$this->services->cacheTag('Cart_Nums_' . $uid)->clear();
			return app('json')->successful('ok', ['cartId' => $cartIds, 'cartNum' => $cartNum]);
		}
    }

    /**
     * 购物车 删除商品
     * @param Request $request
     * @return mixed
     */
    public function del(Request $request)
    {
        [$ids, $store_id] = $request->postMore([
            ['ids', ''],//购物车编号
            ['store_id',0]
        ], true);
        $ids = is_array($ids) ? $ids : stringToIntArray($ids);
        if (!count($ids))
            return app('json')->fail('参数错误!');
		$uid = (int)$request->uid();
		$res = $this->services->removeUserCart($uid, $ids);
        if ($res) {
        	$invalid_key = 'invalid_' . $store_id . '_'. $uid;
			CacheService::redisHandler()->delete($invalid_key);
			return app('json')->successful();
        }
        return app('json')->fail('清除失败！');
    }

    /**
     * 购物车 修改商品数量
     * @param Request $request
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function num(Request $request)
    {
        $where = $request->postMore([
            ['id', 0],//购物车编号
			['type', 1],//1购物车 id,2商品 id
            ['number', 0],//购物车编号
        ]);
        if (!$where['id'] || !$where['number'] || !is_numeric($where['id']) || !is_numeric($where['number'])) return app('json')->fail('参数错误!');
		$uid = (int)$request->uid();
		if ($where['type'] == 2) {
			$where['id'] = $this->services->value(['product_id' => $where['id'], 'status' => 1, 'uid' => $uid, 'staff_id' => 0], 'id');
		}
		unset($where['type']);
        $res = $this->services->changeUserCartNum($where['id'], $where['number'], $uid);
        if ($res) return app('json')->successful();
        else return app('json')->fail('修改失败');
    }

    /**
     * 购物车 统计 数量 价格
     * @param Request $request
     * @return mixed
     */
    public function count(Request $request, SystemStoreServices $storeServices)
    {
	    [$numType, $store_id,$cat] = $request->postMore([
		    ['numType', true],//购物车编号
		    ['store_id', 0],
		    ['cat', 1]
        ], true);
		if (!$store_id || $store_id == 'undefined') $store_id = 0;
		$uid = $request->hasMacro('uid') ? (int)$request->uid() : 0;
		$result = ['count' => 0, 'ids' => [], 'sum_price' => 0];
		if ($uid) {
			$where = ['status' => 1, 'staff_id' => 0,'cat'=>$cat,'store_id'=>$store_id];
//			$entryRules = $storeServices->getStoreIdByEntryRules($uid);
//			$storeId = $entryRules['store_id'] ?? 0;
//			$shop_operation_type = $entryRules['shop_operation_type'] ?? 1;
//			switch ($shop_operation_type) {
//				case 1://展示平台加入购物车商品
//					$where['store_id'] = 0;
//					break;
//				case 2://展示平台+门店加入购物车商品
//					$where['store_id'] = [0, $storeId];
//					break;
//				case 3://展示平台加入购物车商品
//					$where['store_id'] = $storeId;
//					break;
//				default://展示平台加入购物车商品
//					$where['store_id'] = 0;
//					break;
//			}
			$result = $this->services->getUserCartCount($uid, $where, $numType);
		}
        return app('json')->success($result);
    }
    public function counts(Request $request, SystemStoreServices $storeServices)
    {
        [$numType, $store_id,$cat]  = $request->postMore([
            ['numType', true],//购物车编号
            ['store_id', 1],
	        ['cat', 0]
        ], true);
		if (!$store_id || $store_id == 'undefined') $store_id = 0;
		$uid = $request->hasMacro('uid') ? (int)$request->uid() : 0;
		$result = ['count' => 0, 'ids' => [], 'sum_price' => 0];
		if ($uid) {
			if ($cat>0){
				$where = ['status' => 1, 'staff_id' => 0,'cat'=>$cat];
			}
			$entryRules = $storeServices->getStoreIdByEntryRules($uid);
			$storeId = $entryRules['store_id'] ?? 0;
			$shop_operation_type = $entryRules['shop_operation_type'] ?? 1;
			switch (3) {
				case 1://展示平台加入购物车商品
					$where['store_id'] = 0;
					break;
				case 2://展示平台+门店加入购物车商品
					$where['store_id'] = [0, $storeId];
					break;
				case 3://展示平台加入购物车商品
					$where['store_id'] = $storeId;
					break;
				default://展示平台加入购物车商品
					$where['store_id'] = 0;
					break;
			}
			$result = $this->services->getUserCartCount($uid, $where, $numType);
		}
        return app('json')->success($result);
    }

    /**
     * 购物车重选
     * @param Request $request
     * @return mixed
     */
    public function reChange(Request $request)
    {
        [$cart_id, $product_id, $unique] = $request->postMore([
            ['cart_id', 0],
            ['product_id', 0],
            ['unique', '']
        ], true);
        $this->services->modifyCart($cart_id, $product_id, $unique);
        return app('json')->success('重选成功');
    }

	/**
 	* 计算用户购物车商品（优惠活动、最优优惠券）
	* @param Request $request
	* @return mixed
	*/
	public function computeCart(Request $request)
	{
		[$cartId, $new, $addressId, $shipping_type, $storeId] = $request->postMore([
            'cartId',
            'new',
            ['addressId', 0],
            ['shipping_type', -1],
            ['store_id', 0],
            ['delivery_type', 1],
        ], true);
        if (!is_string($cartId) || !$cartId) {
           $result = ['promotions' => [], 'coupon' => [], 'deduction' => [], 'svip_status' => false, 'svip_price' => 0.00];
        } else {
			$uid = (int)$request->uid();
			$result = $this->services->computeUserCart($uid, $cartId, !!$new, (int)$addressId, (int)$shipping_type, (int)$storeId);
        }
        return app('json')->success($result);
	}
}
